<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="50%"
    top="5%"
  >
    <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="任务名称" name="jobName">
        <t-input clearable v-model="formData.jobName" placeholder="请输入任务名称"/>
      </t-form-item>
      <t-form-item label="Bean名称" name="beanName">
        <t-input clearable v-model="formData.beanName" placeholder="请输入Bean名称"/>
      </t-form-item>
      <t-form-item label="方法名称" name="methodName">
        <t-input clearable v-model="formData.methodName" placeholder="请输入方法名称"/>
      </t-form-item>
      <t-form-item label="参数" name="parameter">
        <t-input clearable v-model="formData.parameter" placeholder="请输入参数"/>
      </t-form-item>
      <t-form-item label="cron表达式" name="cronExpression">
        <t-input-adornment style="width: 100%" >
          <t-input clearable v-model="formData.cronExpression"/>
          <template #append>
            <t-button  theme="default" variant="outline" @click="dialog.openCron=true">
              生成表达式
              <t-icon name="time" slot="icon"/>
            </t-button>
          </template>
        </t-input-adornment>
      </t-form-item>
      <t-form-item label="状态" name="jobState">
        <t-select style="width: 240px" placeholder="状态" :options="dict.type.job_state"
                  v-model="formData.jobState"></t-select>
      </t-form-item>
      <t-form-item label="备注" name="remark">
        <t-textarea
          clearable :autosize="{minRows: 3}"
          v-model="formData.remark"
          placeholder="请输入内容"
        />
      </t-form-item>
    </t-form>
    <crontab @fill="crontabFill" :expression="formData.cronExpression" :visible.sync="dialog.openCron" v-if="dialog.openCron"></crontab>
  </t-dialog>
</template>

<script>
import Crontab from '@/components/Crontab/index.vue'

export default {
  name: "add-edit-job",
  components:{
    Crontab
  },
  props: ['header', 'objId', 'parentId'],
  dicts: ["job_state"],
  data() {
    return {
      dialog: {
        openCron: false
      },
      formData: {
        jobName: '',
        beanName: '',
        methodName: '',
        parameter: '',
        cronExpression: '',
        jobState: '1',
        remark: '',
      },
      rules: {
        jobName: [
          {required: true, message: "任务名称不能为空"}
        ],
        beanName: [
          {required: true, message: "Bean名称不能为空"}
        ],
        methodName: [
          {required: true, message: "方法名称不能为空"}
        ],
        cronExpression: [
          {required: true, message: "cron表达式不能为空"}
        ],
        jobState: [
          {required: true, message: "状态不能为空"}
        ],
      },
      visible: true,
      loading: false,
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods: {
    getObj() {
      this.$api.system.monitor.jobGet(this.objId).then(response => {
        Object.assign(this.formData, response.data)
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.system.monitor.jobUpdate(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.monitor.jobAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
    crontabFill(value) {
      this.formData.cronExpression = value;
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>
</style>
